$(function() {
	//加载更多
	var span = $("#woyaoqingjia>article>.jiazai>span");
	span.hide();
	//默认填写数据
	$("#wode_qingjia").click(function() {
		new Ajax("/yiyuekecheng").keyvalue("type", "1").getJson(function(json) {
			chaxun(json, 1);
		})
	})

	//获取要填充的区域
	var html = $("#woyaoqingjia>article>ul");
	//选择请假课程与请假记录
	$("#woyaoqingjia>article").find("a").click(function() {
		html.empty();

		$(this).addClass("intro");
		$(this).siblings().removeClass("intro");

		if($(this).text() == "请假课程") {
			span.hide();
			new Ajax("/yiyuekecheng").keyvalue("type", "1").getJson(function(json) {
				chaxun(json, 1);
			})
		} else {
			span.show();
			new Ajax("/qingjiajilu").keyvalue("type", "1").getJson(function(json) {
				chaxun(json, 2);
			})
		}
	})
	//计算开课时间转化为秒
	function shi(join) {
		var le = join.length;
		var shi, feng, t;
		if(le == 3) {
			shi = join.substring(0, 1) * 3600;
			feng = join.substring(1, 3) * 60;
			t = shi + feng;
		} else {
			shi = join.substring(0, 2) * 3600;
			feng = join.substring(2, 4) * 60;
			t = shi + feng;
		}
		return t;
	}
	//字符串拼接
	function joint(join) {
		var le = join.length;
		var str;
		if(le == 3) {
			str = join.substring(0, 1) + ":";
			str = str + join.substring(1, 3);
		} else {
			str = join.substring(0, 2) + ":";
			str = str + join.substring(2, 4);
		}
		return str;
	}
	//判断请假是否批准
	function state(e) {
		var zhuangtai = "";
		switch(e) {
			case "1":
				zhuangtai = "待审核";
				break;
			case "2":
				zhuangtai = "通过";
				break;
			case "3":
				zhuangtai = "失败";
				break;
		}
		return zhuangtai;
	}

	//将函数计算的周几进行解析
	function zhou(e) {
		switch(e) {
			case 1:
				txt = "周一";
				break;
			case 2:
				txt = "周二";
				break;
			case 3:
				txt = "周三";
				break;
			case 4:
				txt = "周四";
				break;
			case 5:
				txt = "周五";
				break;
			case 6:
				txt = "周六";
				break;
			case 0:
				txt = "周日";
				break;
		}
		return txt;
	}
	//填写数据
	function chaxun(json, select) {

		var txt = "",
			shijian = "",
			week = "",
			zhuangtai = "";
		JSON.stringify(json.sort(function(x, y) {
			var t1, t2;
			t1 = shi(x["start"]);
			t2 = shi(y["start"]);
			var x = x["time"] + t1,
				y = y["time"] + t2;
			if(x == y) {
				return 0;
			}
			return x - y;
		}))
		$.each(json, function(key, val) {
			shijian = joint(val["start"]);
			week = zhou(new Date(val["time"] * 1000).getDay());
			var z = new Date(val["time"] * 1000).toLocaleString().substr(5, 5);
			if(select == "1") {
				txt += "<li><h1>" + z + "<span>" + week + "</span></h1><h2>" + val["name"] + "</h2><h3>" + shijian + "</h3><h5 xianxiapeixunid='" + val["xianxiapeixunid"] + "'  card='" + val["cardid"] + "'>请假</h5></li>"
			} else {
				zhuangtai = state(val["leavestate"]);
				txt += "<li><h1>" + z + "<span>" + week + "</span></h1><h2>" + val["name"] + "</h2><h3>" + shijian + "</h3><h5 style='background:white; color:#96c72e'>" + zhuangtai + "</h5></li>"
			}

		});
		html.html(txt);
		//输入请假内容
		var textarea = $("#woyaoqingjia>article>.yycg");
		var sub = $("#woyaoqingjia>article>.yycg>button");
		var wenzi = "";
		$("#woyaoqingjia>article>ul>li").find("h5").click(function() {
			wenzi = $(this);
			var car = $(this).attr("card");
			var dan = $(this).attr("xianxiapeixunid");

			new Ajax("/qingjia").keyvalue("type", "1").keyvalue("cardid", car).keyvalue("courseid", dan).getJson(function(json) {
				if(json == "1") {
					alert("您已近对于该课程申请过请假，请到请假记录查看");
				} else {
					sub.attr("card", car);
					sub.attr("dingdan", dan);
					textarea.show();
				}
			})
		})
		//点击提交按钮之后
		$("#qingjia").click(function() {
			var tx = $(this).prev().val();
			new Ajax("/qingjia").keyvalue("reason", tx).keyvalue("cardid", $(this).attr("card")).keyvalue("courseid", $(this).attr("dingdan")).get();
			wenzi.text("已申请").css({
				"background": "#ccc"
			});
			textarea.hide();

		})
		$("#no").click(function() {
			textarea.hide();
		})
	}
	//加载更多
	span.click(function() {
		new Ajax("/qingjiajilu").keyvalue("type", "2").getJson(function(json) {
			chaxun(json, 2);
		})
	})
})